package shell.view {
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.events.ModuleEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.components.FlexModuleLoader;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.mediator.FlexMediator;	
	
	public class FB_HelloWorldMediator extends FlexMediator 
	{
		public static const NAME : String = "FB_HelloWorldMediator";
		
		private var moduleLoader:FlexModuleLoader;
		private var secondModuleLoader:FlexModuleLoader;

		public function FB_HelloWorldMediator(viewComponent : FB_HelloWorld) 
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			app.loadModuleOne.addEventListener(MouseEvent.CLICK, handleLoadingModuleOne);
			//app.loadModuleTwo.addEventListener(MouseEvent.CLICK, handleLoadingModuleTwo);
			//app.showHelloWorld.addEventListener(MouseEvent.CLICK, handleButtonClick);
			//app.msg2Module2.addEventListener(MouseEvent.CLICK, handleSecondButtonClick);
		}
		
		private function handleLoadingModuleOne(e:MouseEvent):void
		{
			moduleLoader = new FlexModuleLoader();
			moduleLoader.url = "SimpleModule.swf";
			moduleLoader.router = applicationRouter;
			moduleLoader.loadModule();
			
			moduleLoader.addEventListener(ModuleEvent.READY, handleModuleReady);
		}
		
		private function handleLoadingModuleTwo(e:MouseEvent):void
		{
			secondModuleLoader = new FlexModuleLoader();
			secondModuleLoader.url = "SecondModule.swf";
			secondModuleLoader.router = applicationRouter;
			secondModuleLoader.loadModule();
			
			//secondModuleLoader.addEventListener(ModuleEvent.READY, handleSecondModuleReady);
		}
		
		/* private function handleSecondModuleReady(e:ModuleEvent):void
		{
			app.mySecondCanvas.addChild(e.target as DisplayObject);
		} */
		
		private function handleModuleReady(e:ModuleEvent):void
		{
			trace("kichcha --> module ready");
			routeNotification("sayHelloMessage",null);
			app.myCanvas.addChild(e.target as DisplayObject);
		}
		
		public function handleButtonClick(event:MouseEvent):void
		{
			routeNotification("buttonSelected",null);
		}
		
		private function handleSecondButtonClick(e:MouseEvent):void
		{
			routeNotification("secondButtonClicked", null);
		}
	
		/* public function respondToMessageToShell(note:INotification):void
		{
			app.helloWorldText1.text = "Message From Module 2";
		} */
		
		/* public function respondToModuleButtonClicked(notification:INotification):void
		{
			app.helloWorldText.text = "Message From Module 1";
		} */
		
		public function get app(): FB_HelloWorld
		{
			return viewComponent as FB_HelloWorld;
		}
	}
}